﻿using JewererBasic.BO;
using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JewererBasic.Classes
{
    public class ViewModelConverter
    {
        #region create functions
        public static List<PRODUCT_STONE> Convert(List<StoneView> stones)
        {
            if (stones == null)
                return null;
            List<PRODUCT_STONE> result = new List<PRODUCT_STONE>();
            foreach (var item in stones)
            {
                PRODUCT_STONE ps = new PRODUCT_STONE();
                ps.IdStone = item.Item;
                ps.Qty = item.Qty;
                ps.Weight = item.Weight;
                result.Add(ps);
            }
            return result;
        }

        public static List<PRODUCT_COMPONENT> Convert(List<ComponentView> components)
        {
            if (components == null || components.Count < 1)
                return null;
            List<PRODUCT_COMPONENT> result = new List<PRODUCT_COMPONENT>();
            foreach (var item in components)
            {
                PRODUCT_COMPONENT pc = new PRODUCT_COMPONENT();
                pc.IdComponent = item.Item;
                pc.Qty = item.Qty;
                pc.Weight = item.Weight;
                result.Add(pc);
            }
            return result;
        }

        public static List<PRODUCT_PROCESS> Convert(List<ProcessView> processes)
        {
            if (processes == null)
                return null;
            List<PRODUCT_PROCESS> result = new List<PRODUCT_PROCESS>();
            foreach (var item in processes)
            {
                PRODUCT_PROCESS pp = new PRODUCT_PROCESS();
                pp.Time = item.Time;
                pp.IdProcess = ProcessBO.GetId(item.NameProcess);
                result.Add(pp);
            }
            return result;
        }

        public static List<PRODUCT_MOULD> Convert(List<CastingView> castings)
        {
            if (castings == null)
                return null;
            List<PRODUCT_MOULD> result = new List<PRODUCT_MOULD>();
            foreach (var item in castings)
            {
                PRODUCT_MOULD casting = new PRODUCT_MOULD();
                casting.IdType=TypeBO.GetId(item.Type);
                casting.IdUnit=UnitBO.GetId(item.Unit);
                casting.IdMould = item.IdMould;
                casting.Qty=item.Qty;
                casting.IdProduct = item.IdProduct;
                result.Add(casting);
            }
            return result;
        }

        public static PRODUCT Convert(ProductView pro)
        {
            if (pro == null)
                return null;
            PRODUCT result = new PRODUCT();
            result.Id = pro.Id;
            if (string.IsNullOrEmpty(pro.Drawing) == false)
                result.Drawing = pro.Drawing;
            result.IdGamma = pro.Gamma;
            result.IdKind = pro.Kind;
            result.IdType = pro.Type;
            result.IdUnit = pro.Unit;
            result.MetalWeight = pro.MetalWeight;
            result.TotalWeight = pro.TotalWeight;
            result.Size = pro.Size;
            result.Remark = pro.Remark;
            return result;
        }
        #endregion

        #region Update functions
        public static List<CastingView> Convert(List<PRODUCT_MOULD> product_casting)
        {
            if (product_casting == null)
                return null;
            List<CastingView> result = new List<CastingView>();
            foreach (var item in product_casting)
            {                
                CastingView castingView = new CastingView();
                castingView.Type = item.TYPE.Name;
                castingView.Unit = item.UNIT.Name;
                castingView.IdMould = item.IdMould;
                castingView.Qty = item.Qty;
                result.Add(castingView);
            }
            return result;
        }

        public static List<StoneView> Convert(List<PRODUCT_STONE> stones)
        {
            if (stones == null)
                return null;
            List<StoneView> result = new List<StoneView>();
            foreach (var item in stones)
            {               
                StoneView stoneView = new StoneView();
                stoneView.Item = item.IdStone;
                stoneView.Qty = item.Qty;
                stoneView.Weight = (float)item.Weight;
                result.Add(stoneView);
            }
            return result;
        }

        public static List<ComponentView> Convert(List<PRODUCT_COMPONENT> components)
        {
            if (components == null)
                return null;
            List<ComponentView> result = new List<ComponentView>();
            foreach (var item in components)
            {                
                ComponentView comView = new ComponentView();
                comView.Item = item.IdComponent;
                comView.Qty = item.Qty;
                comView.Weight = (float)item.Weight;
                result.Add(comView);
            }
            return result;
        }

        public static List<ProcessView> Convert(List<PRODUCT_PROCESS> processes)
        {
            if (processes == null)
                return null;
            List<ProcessView> result = new List<ProcessView>();
            foreach (var item in processes)
            {                
                ProcessView processView = new ProcessView();
                processView.Time = item.Time;
                processView.NameProcess = item.PROCESS.Name;
                result.Add(processView);
            }
            return result;
        }

        #endregion
    }
}